#pragma once

// Qt
#include <QWidget>
#include <QVBoxLayout>
#include <QComboBox>
#include <QTimer>
#include <QLabel>
#include <QDebug>
#include <QScreen>
#include <QGuiApplication>
#include <QX11Info>
#include <QSharedPointer>

// X11
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>

// XCB
#include <xcb/xcb.h>
#include <xcb/render.h>
#include <xcb/composite.h>

class WindowThumbnail : public QWidget
{
public:
    WindowThumbnail(QWidget* parent = nullptr);
    ~WindowThumbnail();
private:
    Display* display;
    xcb_connection_t* c;
    QList<Window> wins;
    QTimer* timer = nullptr;
    QScreen* screen = nullptr;

    // layout
    QVBoxLayout* mainLayout = nullptr;
    QComboBox* combobox = nullptr;
    QLabel* pictureBox = nullptr;

    void updateThumbnail(); // update the image
    QImage grabWindowImage(Window); // grab window image

    // X
    QList<Window> getAllWindows();
    bool ewmhIsSupported();
    QString getWindowAtom(Window win, const char *atom);


    inline QString getWindowName(Window win) {
        return getWindowAtom(win, "_NET_WM_NAME");
    }

    inline QString getWindowClass(Window win) {
        return getWindowAtom(win, "WM_CLASS");
    }

};
